/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
void BinaryTreePrevOrder(struct TreeNode* root,int* arr,int* i)
{
    if (root == NULL)
	{
		printf("NULL ");
		return;
	}

	arr[*i] = root->val;
    (*i)++;
	BinaryTreePrevOrder(root->left,arr,i);
	BinaryTreePrevOrder(root->right,arr,i);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize){
    int* arr = (int*)malloc(sizeof(int) * 100);
    if(arr == NULL)
    {
        perror("malloc fail");
        return NULL;
    }

    *returnSize = 0;
    BinaryTreePrevOrder(root,arr,returnSize);
    return arr;
}